<template>
	<div>
		<!-- 入库材料信息 -->
		<div style="width: 100%;height: 15vh;background-color: #4142F4">
			<div style="color: #FFFFFF;font-weight: 700;font-size: 38rpx;margin-left: 10%;padding-top: 6%">
				入库
			</div>
		</div>
		<!-- 入库详细信息 -->
		<div id="warehouse_div1">
			<van-cell-group inset>
				<van-field v-model="material_name" name="材料名称" label="材料名称" :placeholder="material_name" readonly />
				<van-field v-model="material_model" name="规格型号" label="规格型号" :placeholder="material_model" readonly />
			</van-cell-group>

			<div @click="getSourceName()">
				<van-field required v-model="material_source" is-link readonly label="供应单位" placeholder="选择供应单位" />
			</div>
			
			<div @click="getManuName()">
				<van-field required v-model="material_manu" is-link readonly label="生产厂家" placeholder="选择生产厂家" />
			</div>

			<div>
				<van-field required v-model="warehouse_time" label="入库时间" placeholder="选择入库时间"
					@input="warehouse_time_input" />
			</div>

			<!-- 甲供编号 -->
			<div>
				<van-field v-model="warehouse_code" label="合格证编号" placeholder="合格证编号(甲供材料必须填写)"
					@input="warehouse_code_input" />
			</div>

			<!-- 材料数量 -->
			<div>
				<van-field required v-model="warehouse_num" label="入库数量" placeholder="入库数量"
					@input="warehouse_num_input" />
			</div>
			<!-- 含税单价 -->
			<div>
				<van-field v-model="warehouse_money" label="含税单价" placeholder="含税单价"
					@input="warehouse_money_input" />
			</div>
			<!-- 税率 -->
			<div>
				<van-field v-model="warehouse_cess" label="税率" placeholder="税率"
					@input="warehouse_cess_input" />
			</div>
			<!-- 运费 -->
			<div>
				<van-field v-model="warehouse_freight" label="运费" placeholder="运费" @input="warehouse_freight_input" />
			</div>
			<!-- 车牌识别 -->
			<div>
				<van-field v-model="warehouse_carnum" label="运输车牌" @input="warehouse_carnum_input">
					<van-button color="#4142F4" slot="button" size="small" type="info" custom-class="button"
						@click="upload1()">
						识别
					</van-button>
				</van-field>
			</div>
			<div class="opinion_div3">
				<div v-for="(item, i) in imgArr" :key="i" v-if="a" style="margin-left: 5px;margin-right: 20px;">
					<image :src="imgArr[i]" style="width: 70px;height: 70px;" @click="deleteimg(i)"></image>
				</div>
				<div @click="getImg">
					<image src="../../../static/opinion_add.png" style="width: 70px;height: 70px;"></image>
				</div>
			</div>
			<div>
				<van-action-sheet :show="show" :actions="actions" cancel-text="取消" @close="toggleActionSheet"
					@cancel="toggleActionSheet" @select="deleteyes" />
			</div>
			<!-- 存放地 -->
			<div>
				<van-field v-model="warehouse_place" label="存放地" placeholder="存放地" @input="warehouse_place_input" />
			</div>
			<!-- 备注 -->
			<div>
				<van-field v-model="warehouse_remarks" label="入库备注" placeholder="入库备注" @input="warehouse_remarks_input" />
			</div>

			<!-- 材料照片 -->
			<div class="opinion_div1">
				<div style="font-size: 27rpx;margin-top: 5%;">
					材料照片
				</div>
				<div v-for="(item, i) in imgArr1" :key="i" v-if="a1" style="margin-left: 5%;margin-right: 20px;">
					<image :src="imgArr1[i]" style="width: 70px;height: 70px;" @click="deleteimg1(i)"></image>
				</div>
				<div @click="getImg1" style="margin-left: 5%;">
					<image src="../../../static/opinion_add.png" style="width: 70px;height: 70px;"></image>
				</div>
				<div>
					<van-action-sheet :show="show1" :actions="actions" cancel-text="取消" @close="toggleActionSheet1"
						@cancel="toggleActionSheet1" @select="deleteyes1" />
				</div>
				<van-divider />
			</div>
			<!-- 车辆照片 -->
			<div class="opinion_div1">
				<div style="font-size: 27rpx;margin-top: 5%;">
					车辆照片
				</div>
				<div v-for="(item, i) in imgArr2" :key="i" v-if="a2" style="margin-left: 5%;margin-right: 20px;">
					<image :src="imgArr2[i]" style="width: 70px;height: 70px;" @click="deleteimg2(i)"></image>
				</div>
				<div @click="getImg2" style="margin-left: 5%;">
					<image src="../../../static/opinion_add.png" style="width: 70px;height: 70px;"></image>
				</div>
				<div>
					<van-action-sheet :show="show2" :actions="actions" cancel-text="取消" @close="toggleActionSheet2"
						@cancel="toggleActionSheet2" @select="deleteyes2" />
				</div>
				<van-divider />
			</div>
			<!-- 运单照片 -->
			<div class="opinion_div1">
				<div style="font-size: 27rpx;margin-top: 5%;">
					运单照片
				</div>
				<div v-for="(item, i) in imgArr3" :key="i" v-if="a3" style="margin-left: 5%;margin-right: 20px;">
					<image :src="imgArr3[i]" style="width: 70px;height: 70px;" @click="deleteimg3(i)"></image>
				</div>
				<div @click="getImg3" style="margin-left: 5%;">
					<image src="../../../static/opinion_add.png" style="width: 70px;height: 70px;"></image>
				</div>
				<div>
					<van-action-sheet :show="show3" :actions="actions" cancel-text="取消" @close="toggleActionSheet3"
						@cancel="toggleActionSheet3" @select="deleteyes3" />
				</div>
				<van-divider />
			</div>
			<!-- 提交 -->
			<div style="padding-bottom: 10%;padding-top: 5%;">
				<van-button color="#4142F4" type="primary" block @click="materialWarehouse()">入库</van-button>
			</div>
		</div>
	</div>
</template>

<script>
	import { BaseUrl } from '@/util/util.js';
	const PrefixUrl = BaseUrl();
	var that;
	export default {
		data() {
			return {
				material_id: '',
				material_name: '',
				material_model: '',
				material_source: '',
				showPicker: false,
				warehouse_time: '',
				imgArr: [],
				a: false,
				imgArr1: [],
				a1: false,
				imgArr2: [],
				a2: false,
				imgArr3: [],
				a3: false,
				show: false,
				show1: false,
				show2: false,
				show3: false,
				deleteid: '',
				deleteid1: '',
				deleteid2: '',
				deleteid3: '',
				actions: [{
					name: '删除'
				}],
				carplate: '',
				carnums: [],
				warehouse_carnum: '',
				warehouse_num: '',
				warehouse_money: '',
				warehouse_cess: '',
				warehouse_code: '',
				warehouse_freight: '',
				warehouse_remarks: '',
				warehouse_place: '',
				uuid: '',
				materialPhotoUrl: '',
				carPhotoUrl: '',
				ferightPhotoUrl: '',
				material_manu: ''
			}
		},
		onLoad: function(option) {
			this.material_id = option.id;
			this.material_name = option.name;
			this.material_model = option.model;
			this.material_source = uni.getStorageSync("material_sourecName");
			this.material_manu = uni.getStorageSync("material_manuName");
		},
		created() {
			that = this;
			this.material_source = uni.getStorageSync("material_sourecName");
			this.material_manu = uni.getStorageSync("material_manuName");
			this.warehouse_time = this.format(new Date());
		},
		methods: {
			// uuid
			guuid() {
			    function S4() {
			        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
			    }
			    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
			},
			warehouse_time_input(event) {
				this.warehouse_time = event.detail;
			},
			warehouse_carnum_input(event) {
				this.warehouse_carnum = event.detail;
			},
			warehouse_num_input(event) {
				this.warehouse_num = event.detail;
			},
			warehouse_money_input(event) {
				this.warehouse_money = event.detail;
			},
			warehouse_cess_input(event) {
				this.warehouse_cess = event.detail;
			},
			warehouse_code_input(event) {
				this.warehouse_code = event.detail;
			},
			warehouse_freight_input(event) {
				this.warehouse_freight = event.detail;
			},
			warehouse_place_input(event) {
				this.warehouse_place = event.detail;
			},
			warehouse_remarks_input(event) {
				this.warehouse_remarks = event.detail;
			},
			//格式化时间
			format(dat) {
				//获取年月日，时间
				var year = dat.getFullYear();
				var mon = (dat.getMonth() + 1) < 10 ? "0" + (dat.getMonth() + 1) : dat.getMonth() + 1;
				var data = dat.getDate() < 10 ? "0" + (dat.getDate()) : dat.getDate();
				var hour = dat.getHours() < 10 ? "0" + (dat.getHours()) : dat.getHours();
				var min = dat.getMinutes() < 10 ? "0" + (dat.getMinutes()) : dat.getMinutes();
				var seon = dat.getSeconds() < 10 ? "0" + (dat.getSeconds()) : dat.getSeconds();
				var newDate = year + mon + data + hour + min;
				return newDate;
			},
			getSourceName() {
				uni.redirectTo({
					url: '/pages/material/warehouse/warehouse_source?id=' + this.material_id +
						'&name=' + this.material_name + '&model=' + this.material_model
				});
			},
			getManuName() {
				uni.redirectTo({
					url: '/pages/material/warehouse/warehouse_manu?id=' + this.material_id +
						'&name=' + this.material_name + '&model=' + this.material_model
				});
			},
			getImg() {
				if (this.imgArr.length > 0) {
					uni.showToast({
						title: '只可上传一张',
						duration: 1500,
						mask: true,
						icon: 'none'
					})
					return;
				} else {
					let index = 0;
					uni.chooseImage({
						count: 1, //默认9
						sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
						sourceType: ['album', 'camera'], //从相册选择
						success: function(res) {
							if (that.imgArr.length == 0) {
								that.a = true
								var tempFilePaths = res.tempFilePaths;
								for (var i = 0; i < tempFilePaths.length; i++) {
									that.imgArr[i] = tempFilePaths[i]
								}
							} else {
								that.a = false
								var tempFilePaths = res.tempFilePaths;
								for (var i = 0; i < tempFilePaths.length; i++) {
									that.imgArr[that.imgArr.length] = tempFilePaths[i]
								}
								that.a = true
							}
						}
					})
				}

			},
			getImg1() {
				if (this.imgArr1.length > 0) {
					uni.showToast({
						title: '只可上传一张',
						duration: 1500,
						mask: true,
						icon: 'none'
					})
					return;
				} else {
					let index = 0;
					uni.chooseImage({
						count: 1, //默认9
						sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
						sourceType: ['album', 'camera'], //从相册选择
						success: function(res) {
							if (that.imgArr1.length == 0) {
								that.a1 = true
								var tempFilePaths = res.tempFilePaths;
								for (var i = 0; i < tempFilePaths.length; i++) {
									that.imgArr1[i] = tempFilePaths[i]
								}
							} else {
								that.a1 = false
								var tempFilePaths = res.tempFilePaths;
								for (var i = 0; i < tempFilePaths.length; i++) {
									that.imgArr1[that.imgArr1.length] = tempFilePaths[i]
								}
								that.a1 = true
							}
						}
					})
				}

			},
			getImg2() {
				if (this.imgArr2.length > 0) {
					uni.showToast({
						title: '只可上传一张',
						duration: 1500,
						mask: true,
						icon: 'none'
					})
					return;
				} else {
					let index = 0;
					uni.chooseImage({
						count: 1, //默认9
						sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
						sourceType: ['album', 'camera'], //从相册选择
						success: function(res) {
							if (that.imgArr2.length == 0) {
								that.a2 = true
								var tempFilePaths = res.tempFilePaths;
								for (var i = 0; i < tempFilePaths.length; i++) {
									that.imgArr2[i] = tempFilePaths[i]
								}
							} else {
								that.a2 = false
								var tempFilePaths = res.tempFilePaths;
								for (var i = 0; i < tempFilePaths.length; i++) {
									that.imgArr2[that.imgArr2.length] = tempFilePaths[i]
								}
								that.a2 = true
							}
						}
					})
				}

			},
			getImg3() {
				if (this.imgArr3.length > 0) {
					uni.showToast({
						title: '只可上传一张',
						duration: 1500,
						mask: true,
						icon: 'none'
					})
					return;
				} else {
					let index = 0;
					uni.chooseImage({
						count: 1, //默认9
						sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
						sourceType: ['album', 'camera'], //从相册选择
						success: function(res) {
							if (that.imgArr3.length == 0) {
								that.a3 = true
								var tempFilePaths = res.tempFilePaths;
								for (var i = 0; i < tempFilePaths.length; i++) {
									that.imgArr3[i] = tempFilePaths[i]
								}
							} else {
								that.a3 = false
								var tempFilePaths = res.tempFilePaths;
								for (var i = 0; i < tempFilePaths.length; i++) {
									that.imgArr3[that.imgArr3.length] = tempFilePaths[i]
								}
								that.a3 = true
							}
						}
					})
				}

			},

			upload1() {
				if(this.imgArr.length > 0){
					uni.showLoading({
						title: '识别中.....'
					});
					wx.uploadFile({
						// 需要上传的地址
						url: 'https://www.cr12cz.cn/czscsms/up/upload',
						// filePath  需要上传的文件
						filePath: this.imgArr[0],
						name: 'file',
						header: {
							'content-type': 'multipart/form-data'
						},
						formData: {
							userid: uni.getStorageSync("userid")
						},
						success: function(res1) {
							const url_data = JSON.parse(res1.data);
							// 显示上传信息
							uni.request({
								method: 'POST',
								url: 'https://api03.aliyun.venuscn.com/ocr/car-license',
								data: {
									pic: url_data.obj
								},
								header: {
									"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
									'Authorization': 'APPCODE ef6d2fd3878543a7820f0b90b5051838'
								},
								success: (res3) => {
									if (res3.data.data.number) {
										uni.hideLoading();
										that.warehouse_carnum = res3.data.data.number;
					
									}
								}
							})
						},
						fail: function(res2) {
							console.log(res2)
						}
					})
				}else {
					uni.showToast({
						title: '请选择车牌照片!',
						mask: true,
						icon: 'none'
					})
				}
				

			},
			deleteimg(i) {
				this.show = !this.show
				this.deleteid = i
			},
			deleteimg1(i) {
				this.show1 = !this.show1
				this.deleteid1 = i
			},
			deleteimg2(i) {
				this.show2 = !this.show2
				this.deleteid2 = i
			},
			deleteimg3(i) {
				this.show3 = !this.show3
				this.deleteid3 = i
			},
			toggleActionSheet() {
				this.show = !this.show
			},
			toggleActionSheet1() {
				this.show1 = !this.show1
			},
			toggleActionSheet2() {
				this.show2 = !this.show2
			},
			toggleActionSheet3() {
				this.show3 = !this.show3
			},
			deleteyes() {
				this.imgArr.splice(this.deleteid, 1)
				if (this.imgArr.length == 0) {
					this.a = false
				}
			},
			deleteyes1() {
				this.imgArr1.splice(this.deleteid1, 1)
				if (this.imgArr1.length == 0) {
					this.a1 = false
				}
			},
			deleteyes2() {
				this.imgArr2.splice(this.deleteid2, 1)
				if (this.imgArr2.length == 0) {
					this.a2 = false
				}
			},
			deleteyes3() {
				this.imgArr3.splice(this.deleteid3, 1)
				if (this.imgArr3.length == 0) {
					this.a3 = false
				}
			},
			materialWarehouse() {
				this.uuid = this.guuid();
				if (this.imgArr1.length > 0){
					this.uploadMaterialPhoto(this.uuid);
				}
				if (this.imgArr2.length > 0){
					this.uploadcarPhoto(this.uuid);
				}
				if (this.imgArr3.length > 0){
					this.uploadfergihtPhoto(this.uuid);
				}
				if (this.material_source === ''){
					uni.showToast({
						title: '请选择供应单位!',
						mask: true,
						icon: 'none'
					})
				}else{
					uni.request({
						method: 'POST',
						url: PrefixUrl + '/materialWarehouse/materialWarehouse',
						data: {
							id: this.uuid,
							material_id: this.material_id,
							material_source: this.material_source,
							warehouse_time: this.warehouse_time,
							warehouse_carnum: this.warehouse_carnum,
							material_num: this.warehouse_num,
							material_money: this.warehouse_money,
							cess: this.warehouse_cess,
							material_freight: this.warehouse_freight,
							warehouse_place: this.warehouse_place,
							material_remarks: this.warehouse_remarks,
							sbpa_code: this.warehouse_code,
							up_user: uni.getStorageSync('username'),
							up_bdst: uni.getStorageSync('userbdst'),
							material_name: this.material_name,
							manu: this.material_manu,
							model: this.material_model
					
						},
						header: {
							'content-type': 'application/x-www-form-urlencoded',
							'token': uni.getStorageSync('token')
						},
						success: (res) => {
							if (res.data.obj){
								uni.navigateBack({
									delta: 1
								});
								uni.showToast({
									title: '入库成功!',
									mask: true,
									icon: 'none'
								})
							}
						}
					});
				}
				
			},
			
			// 上传材料照片
			uploadMaterialPhoto(uuid){
				wx.uploadFile({
					// 需要上传的地址
					url: 'https://www.cr12cz.cn/czscsms/up/upload',
					// filePath  需要上传的文件
					filePath: this.imgArr1[0],
					name: 'file',
					header: {
						'content-type': 'multipart/form-data'
					},
					formData: {
						userid: uni.getStorageSync("userid")
					},
					success: function(res1) {
						const url_data = JSON.parse(res1.data);
						uni.request({
							method: 'POST',
							url: PrefixUrl + '/materialPhoto/saveMaterialPhoto',
							data: {
								warehouse_id: uuid,
								flag: 0,
								photo_type: '材料照片',
								photo_url: url_data.obj
						
							},
							header: {
								'content-type': 'application/x-www-form-urlencoded',
								'token': uni.getStorageSync('token')
							},
							success: (res) => {
								this.materials = res.data.obj;
							}
						});
					},
					fail: function(res2) {
						console.log(res2)
					}
				})
			},
			// 上传运单照片
			uploadfergihtPhoto(uuid){
				wx.uploadFile({
					// 需要上传的地址
					url: 'https://www.cr12cz.cn/czscsms/up/upload',
					// filePath  需要上传的文件
					filePath: this.imgArr3[0],
					name: 'file',
					header: {
						'content-type': 'multipart/form-data'
					},
					formData: {
						userid: uni.getStorageSync("userid")
					},
					success: function(res1) {
						const url_data = JSON.parse(res1.data);
						uni.request({
							method: 'POST',
							url: PrefixUrl + '/materialPhoto/saveMaterialPhoto',
							data: {
								warehouse_id: uuid,
								flag: 0,
								photo_type: '运单照片',
								photo_url: url_data.obj
						
							},
							header: {
								'content-type': 'application/x-www-form-urlencoded',
								'token': uni.getStorageSync('token')
							},
							success: (res) => {
								this.materials = res.data.obj;
							}
						});
					},
					fail: function(res2) {
						console.log(res2)
					}
				})
			},
			// 上传车辆照片
			uploadcarPhoto(uuid){
				wx.uploadFile({
					// 需要上传的地址
					url: 'https://www.cr12cz.cn/czscsms/up/upload',
					// filePath  需要上传的文件
					filePath: this.imgArr2[0],
					name: 'file',
					header: {
						'content-type': 'multipart/form-data'
					},
					formData: {
						userid: uni.getStorageSync("userid")
					},
					success: function(res1) {
						const url_data = JSON.parse(res1.data);
						console.log(url_data)
						uni.request({
							method: 'POST',
							url: PrefixUrl + '/czsczq/materialPhoto/saveMaterialPhoto',
							data: {
								warehouse_id: uuid,
								flag: 0,
								photo_type: '车辆照片',
								photo_url: url_data.obj
						
							},
							header: {
								'content-type': 'application/x-www-form-urlencoded',
								'token': uni.getStorageSync('token')
							},
							success: (res) => {
								this.materials = res.data.obj;
							}
						});
					},
					fail: function(res2) {
						console.log(res2)
					}
				})
			}
			
		}
	}
</script>

<style>
	#warehouse_div1 {
		position: absolute;
		top: 8vh;
		width: 92%;
		height: 100vh;
		background-color: #FFFFFF;
		left: 4%;
		border-radius: 20rpx;
		padding-top: 5%;
	}

	.warehouse_div1_font {
		margin-left: 5%;
		font-size: 30rpx;
		margin-bottom: 5%;
	}

	.opinion_div3 {
		margin-left: 24%;
		width: 100%;
		display: flex;
	}

	.opinion_div1 {
		margin-left: 4.5%;
		width: 100%;
		display: flex;
	}
</style>
